<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时不可以修改本文件，影响升级覆盖
 */

class Tuijian extends \Phpcmf\Home\Module
{

    public function index() {
		//先查询 1_quan_flag 表中的id 循环查询 1_quan 表中的数据
        
        // 先查询1_quan_flag表获取所有id
        $flag_ids = \Phpcmf\Service::M()->table('1_quan_flag')
            ->select('id')
            ->getAll();
        
        $list_data = [];
        
        // 循环查询1_quan表中的数据
        foreach ($flag_ids as $flag) {
            $flag_id = $flag['id'];
            
            // 根据flag的id查询对应的1_quan表数据
            $quan_data = \Phpcmf\Service::M()->table('1_quan')
                ->where('id', $flag_id)
                ->where('fstatus', 1) // 只查询状态为1的
                ->getRow();
                	//贴数
		    $tieshu = \Phpcmf\Service::M()->db->table('1_quan_form_quantie')->where('cid', $flag_id)->where('status', 1)->countAllResults();
    		//圈子会员数量
    		$huiyuan = \Phpcmf\Service::M()->db->table('1_quan_jiaquan')->where('quanid', $flag_id)->countAllResults();
            //帖子数
		    $tie_data = \Phpcmf\Service::M()->table('1_quan_form_quantie')->where('cid', $flag_id)->where('status', 1)->order_by('id desc')->limit(4)->getAll(); 
			//循环帖子数据
			$tie = [];
			foreach($tie_data as $t){
				$tie[] = [
					'tie_id' => $t['id'],
					'quan_id' => $quan_data['id'],
					'tie_uid' => $t['uid'],
					'tie_title' => $t['title'],
				];
			}
            
            // 如果找到了对应的圈子数据
            if ($quan_data) {
                // 获取完整的flag信息
                $flag_info = \Phpcmf\Service::M()->table('1_quan_flag')
                    ->where('id', $flag_id)
                    ->getRow();
                
                $list_data[] = [
        			'quan_id' => $quan_data['id'],
        			'quan_cid' => $quan_data['catid'],
        			'quan_uid' => $quan_data['uid'],
        			'quan_ming' => $quan_data['title'],
        			'jiage' => $quan_data['jiage'],
        			'quan_tubiao' => dr_get_file($quan_data['thumb']),
        			'quan_tieshu' => $tieshu, //圈子帖子数量
					'quan_huiyuan' => $huiyuan, //圈子会员数量
        			'quantie_total' => $quan_data['quantie_total'],
        			'jiaru_total' => $quan_data['jiaru_total'],
        			'flag_info' => $flag_info, // 关联的1_quan_flag表数据
        			'tie' => $tie,
        		
        		];
            }
        }
        
        // 限制返回数据数量为10条
        $list_data = array_slice($list_data, 0, 10);
        $data = [
			'list' => $list_data,
		];

		$this->_json('1', '读取成功', $data);
       
    }
	public function tie() {
		$tieshu = \Phpcmf\Service::M()->table('1_quan_form_quantie')->where('tuijian', 1)->where('status', 1)->order_by('id desc,inputtime desc')->limit(5)->getAll();
		$data = [
			'list' => $tieshu,
		];
		$this->_json('1', '读取成功', $data);
	}

   
}
